<?php namespace Home\Controller; 
//商品表
use \Common\Model\Goods;
use \Home\Model\Address;
//订单表
use \Home\Model\Order;
//订单列表
use \Home\Model\OrderList;
use \Home\Model\User;
//购物车控制器
class CartController extends CommonController{
//  首页
    public function index(){
		if(!empty($_SESSION['cart'])){
		$data = $_SESSION['cart']['goods'];
			if(!empty($data)){
				View::with('data',$data);
			}else{
				View::with('data',[]);
				go('Cart/noCart');
			}
		}
		//统计价格
		$count = Cart::getTotalPrice();
		View::with('count',$count);
        View::make();
    }
	//删除物品
	public function del(){
		$key = Q("get.key");
		unset($_SESSION['cart']['goods'][$key]);
		if(empty($_SESSION['cart']['goods'])){
			unset($_SESSION['cart']);
		}
		View::success("删除成功",U('Cart/index'));
	}
	//空购物车
	public function noCart(){
	    View::make();
	}
//  结算(填写订单)
    public function closed(){
    	$uid = $_SESSION['user']['uid'];
    	$addressModel = new Address();
		//查询
		$addrData = $addressModel->where("user_uid={$uid}")->get();
		View::with('addrData',$addrData);		
		$checkow = Q("post.checkow");
		$data = $_SESSION['cart']['goods'];
		//获得单选中的商品(带上id)
		$keyData = [];
		foreach ($data as $k => $v) {
			foreach ($checkow as $key => $value) {
				if($value==$k){
					$keyData[$k] = $v;
				}
			}
		}
		//价格总计
		$count['countMoney'] = 0;
		foreach ($keyData as $k => $v) {
			$count['countMoney'] =$count['countMoney'] + $v['total'];
		}
		View::with('count',$count);
		View::with('keyData',$keyData);
        View::make();
    }
	//添加地址
	public function address(){
		$addr = Q("post.");
		$uid = $_SESSION['user']['uid'];
		//数组转换成字符串
		$addr['area'] = implode(',', $addr['area']);
		$addr['user_uid']=$uid;
		if(IS_AJAX){
			$addressModel = new Address();
//			添加地址
			$aid = $addressModel->add($addr);
						
			$addData = $addressModel->where("aid={$aid}")->find();

			echo json_encode($addData);
		}

	}
	//完成订单
	public function place(){
		$aid = Q("post.aid");
		if(!$aid) View::error('请填写地址',U('Cart/index'));
		$keys = Q("post.key");
		$data = $_SESSION['cart']['goods'];
		//配送方为
		$keyData = [];
		foreach ($data as $key => $value) {
			foreach ($keys as $k => $v) {
				if($v == $key){
					$keyData[$key]=$value;
				}
			}
		}
		//统计数量
		$num = count($keyData);
		View::with('num',$num);
		//价格总计
		$count['countMoney'] = 0;
		foreach ($keyData as $k => $v) {
			$count['countMoney'] =$count['countMoney'] + $v['total'];
		}
		View::with('count',$count);
//		去重品牌名
		foreach ($keyData as $k => $v) {
			$keyData['bname'][]=$v['options']['bname'];
		}
		//去重品牌名
		$keyData = array_unique($keyData['bname']);
		View::with('keyData',$keyData);
		$data = $_SESSION['cart'];
		//货号id
		$orderId = Cart::getOrderId();
		View::with('orderId',$orderId);
		//获得地址
		
		$addressModel = new Address();
		$addressData = $addressModel->where("aid={$aid}")->find();
		$addressData['number']=$orderId;
		//订单表
		$orderModel = new Order();
		//返回订单自增id
		$oid = $orderModel->store($addressData,Q("post.key"));
		//订单列表
		$orderList = new OrderList();
		$orderList->store($oid);
		unset($_SESSION['cart']);
	    View::make();
	}
	//获得商品总价格
	public function price(){
		$data = Q("post.data");
		$key = $data['key'];
		$data=array(
		    'sid'=>$key,// 唯一 sid，添加购物车时自动生成 
		    'num'=>$data['inpt']
		);
		Cart::update($data);
		$count = $_SESSION['cart']['goods'][$key];
		//获得商品总价格
		$count['countNum'] = Cart::getTotalPrice();
		echo json_encode($count);
	}
	//支付页面
	public function payment(){
		$orderId = Q("get.orderId");
		View::with('orderId',$orderId);
		
		$userModel = new User();
		if(IS_POST){
			$userModel->pay();
		}
	    View::make();
	}
}